﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M008StoreRoomEdit : FormBase
    {
        #region 变量定义

        private XGWMS.BLL.BLL_M008_STOREROOM bll_M008_STOREROOM = new XiaoGang.Soft.XGWMS.BLL.BLL_M008_STOREROOM();
        private MODEL.MOD_M008_STOREROOM model = new XiaoGang.Soft.XGWMS.MODEL.MOD_M008_STOREROOM();
        /// <summary>
        /// 仓库
        /// </summary>
        private const string dataName = "仓库";
        private string _storeRoomID = string.Empty;
        private string _storeRoomManager = string.Empty;
        private string _storeRoomName=string.Empty;
        private string _storeRoomPhone=string.Empty;
        private string _storeRoomAddress=string.Empty;
        private string _storeRoomMemo=string.Empty;
        private string _storeRoomState=string.Empty;

        #endregion

        #region 窗口初始化

        /// <summary>	
        /// <p><b>方法名称</b>SetTabIndex</p>	
        /// <p><b>概要説明	:</b>画面フォーカスの遷移順の設定</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void SetTabIndex()
        {
            int intIndex = 0;           
            txtSTOREROOM_ID.TabIndex = intIndex; intIndex++;
            txtSTOREROOM_NM.TabIndex = intIndex; intIndex++;
            txtSTOREROOM_PHONE.TabIndex = intIndex; intIndex++;
            txtSTOREROOM_ADDRESS.TabIndex = intIndex; intIndex++;
            cmbSTOREROOM_MANAGER.TabIndex = intIndex; intIndex++;
            rdbSTOREROOM_STATE1.TabIndex = intIndex; intIndex++;
            rdbSTOREROOM_STATE2.TabIndex = intIndex; intIndex++;
            txtMEMO.TabIndex = intIndex; intIndex++;    
            btnAdd.TabIndex = intIndex; intIndex++;
            btnClear.TabIndex = intIndex; intIndex++;
            btnExit.TabIndex = intIndex; intIndex++;
        }

        /// <summary>	
        /// <p><b>方法名称</b>ClearControl</p>	
        /// <p><b>概要説明	:</b>画面上の全項目をオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void ClearControl()
        {
            txtSTOREROOM_ID.Text = "";
            txtSTOREROOM_NM.Text = "";
            txtSTOREROOM_PHONE.Text = "";
            txtSTOREROOM_ADDRESS.Text = "";
            if (cmbSTOREROOM_MANAGER.DataSource != null)
            {
                cmbSTOREROOM_MANAGER.SelectedIndex = 0;
            }
            rdbSTOREROOM_STATE1.Checked = true;
            rdbSTOREROOM_STATE2.Checked = false;
            txtMEMO.Text = "";
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetEnable</p>	
        /// <p><b>概要説明  :</b>画面モードのセト</p>
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/06</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        private void SetEnable()
        {
            grpStroeRoomInfo.Enabled = true;
            //txtSTOREROOM_ID.Enabled = true;
            txtSTOREROOM_NM.Enabled = true;
            txtSTOREROOM_PHONE.Enabled = true;
            txtSTOREROOM_ADDRESS.Enabled = true;
            cmbSTOREROOM_MANAGER.Enabled = true;
            rdbSTOREROOM_STATE1.Enabled = true;
            rdbSTOREROOM_STATE2.Enabled = true;
            txtMEMO.Enabled = true;
            btnAdd.Enabled = true;
            btnExit.Enabled = true;
        }

        /// <summary>
        /// <p><b>方法名称</b>InitStoreRoomManger</p>	
        /// <p><b>概要説明	:</b>Control cmbSTOREROOM_MANAGER の初期化する</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void InitComboBox()
        {
            Method.CmbDataBound("USER_ID", cmbSTOREROOM_MANAGER);
            cmbSTOREROOM_MANAGER.SelectedIndex = 0;
        }

        /// <summary>
        /// <p><b>方法名称</b>InitProcess</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// </summary>
        private void InitProcess()
        {
            ClearControl();
            SetTabIndex();            
            SetEnable();
            InitComboBox();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 构造函数
        /// </summary>
        public M008StoreRoomEdit()
        {
            InitializeComponent();
            txtSTOREROOM_ID.Enabled = false;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="StoreRoomID"></param>
        /// <param name="StoreRoomName"></param>
        /// <param name="StoreRoomPhone"></param>
        /// <param name="StoreRoomAddress"></param>
        /// <param name="StoreRoomManager"></param>
        /// <param name="StoreRoomState"></param>
        /// <param name="StoreRoomMemo"></param>
        public M008StoreRoomEdit(string StoreRoomID,string StoreRoomName,string StoreRoomPhone,string StoreRoomAddress
            ,string StoreRoomManager,string StoreRoomState,string StoreRoomMemo)
        {
            InitializeComponent();
            txtSTOREROOM_ID.Enabled = false;
            _storeRoomID = StoreRoomID;
            _storeRoomName = StoreRoomName;
            _storeRoomPhone = StoreRoomPhone;
            _storeRoomAddress = StoreRoomAddress;
            _storeRoomMemo = StoreRoomMemo;
            _storeRoomState = StoreRoomState;
            _storeRoomManager = StoreRoomManager;
            _storeRoomMemo = StoreRoomMemo;
        }
        
        /// <summary>	
        /// <p><b>方法名称</b>CheckProcess</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type = "string" para="strConstrolName">コンポーネントを検査・測定します</param>	
        /// <returns type = "Boolean">　True:成功   False:失敗 </returns>
        protected override bool CheckProcess(string strConstrolName)
        {
            switch (strConstrolName)
            {
                case "txtSTOREROOM_ID":
                    if (Method.CheckIsNumber(txtSTOREROOM_ID.Text.Trim(), 12, false, true) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg,dataName);
                        txtSTOREROOM_ID.Focus();
                        txtSTOREROOM_ID.SelectAll();
                        return false;
                    }
                    break;
                case "txtMEMO":
                    if (Method.CheckIsIncludeSpecailChar(Method.ToDBC(txtMEMO.Text)) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg,dataName);
                        txtMEMO.Focus();
                        txtMEMO.SelectAll();
                        return false;
                    }
                    if (Encoding.Default.GetByteCount(txtMEMO.Text.Trim()) > txtMEMO.MaxLength)
                    {
                        Method.MessageShow("W11007",dataName);
                        txtMEMO.Focus();
                        txtMEMO.SelectAll();
                        return false;
                    }
                    break;
                case "txtSTOREROOM_ADDRESS":
                    if (Method.CheckIsIncludeSpecailChar(Method.ToDBC(txtSTOREROOM_ADDRESS.Text)) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg,dataName);
                        txtSTOREROOM_ADDRESS.Focus();
                        txtSTOREROOM_ADDRESS.SelectAll();
                        return false;
                    }

                    if (Encoding.Default.GetByteCount(txtSTOREROOM_ADDRESS.Text.Trim()) > txtSTOREROOM_ADDRESS.MaxLength)
                    {
                        Method.MessageShow("W11007",dataName);
                        txtSTOREROOM_ADDRESS.Focus();
                        txtSTOREROOM_ADDRESS.SelectAll();
                        return false;
                    }
                    break;
                case "txtSTOREROOM_NM":
                        if (0 == txtSTOREROOM_NM.Text.Length)
                    {
                        Method.MessageShow("W11E02", "仓库名称");//[@]不能为空！
                        txtSTOREROOM_NM.Focus();
                        txtSTOREROOM_NM.SelectAll();
                        return false;
                    }
                    if (Method.CheckIsIncludeSpecailChar(Method.ToDBC(txtSTOREROOM_NM.Text)) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg,dataName);
                        txtSTOREROOM_NM.Focus();
                        txtSTOREROOM_NM.SelectAll();
                        return false;
                    }
                    if (Encoding.Default.GetByteCount(txtSTOREROOM_NM.Text.Trim()) > txtSTOREROOM_NM.MaxLength)
                    {
                        Method.MessageShow("W11007",dataName);
                        txtSTOREROOM_NM.Focus();
                        txtSTOREROOM_NM.SelectAll();
                        return false;
                    }
                    break;
                case "txtSTOREROOM_PHONE":
                    //if (Method.CheckIsNumber(txtSTOREROOM_PHONE.Text.Trim(), 20, true, true) == false)
                    //{
                    //    Method.MessageShow(GlobalVal.glostrMsg,dataName);
                    //    txtSTOREROOM_PHONE.Focus();
                    //    txtSTOREROOM_PHONE.SelectAll();
                    //    return false; 
                    //}
                    if (CheckProcess(txtSTOREROOM_PHONE.Text.Trim(), enumCheckItems.PHONE_NO,
                        true, null) == false)
                    {
                        txtSTOREROOM_PHONE.Focus();
                        txtSTOREROOM_PHONE.SelectAll();
                        return false;
                    }
                    if (Encoding.Default.GetByteCount(txtSTOREROOM_PHONE.Text.Trim()) > txtSTOREROOM_PHONE.MaxLength)
                    {
                        Method.MessageShow("W11007", dataName);
                        txtSTOREROOM_PHONE.Focus();
                        txtSTOREROOM_PHONE.SelectAll();
                        return false;
                    }
                    break;
                case "cmbSTOREROOM_MANAGER":
                    if (0 == cmbSTOREROOM_MANAGER.SelectedIndex )
                    {
                        cmbSTOREROOM_MANAGER.Focus();
                        cmbSTOREROOM_MANAGER.SelectAll();
                        Method.MessageShow("W11015", dataName);
                        return false;
                    }
                    break;
                case "rdbSTOREROOM_STATE1":
                    if (rdbSTOREROOM_STATE1.Checked == false && rdbSTOREROOM_STATE2.Checked == false
                        || rdbSTOREROOM_STATE1.Checked == true && rdbSTOREROOM_STATE2.Checked == true)
                    {
                        Method.MessageShow("W11007", dataName);
                        rdbSTOREROOM_STATE1.Focus();                        
                        return false;
                    }
                    break;
            }
            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckProcessLoop</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/01</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type = "enum" para="enumOperation">コンポーネントを検査・測定します</param>	
        /// <returns type = "Boolean">　True:成功   False:失敗 </returns>
        private bool CheckProcessLoop(enumOperation emOperation)
        {
            switch (emOperation)
            {
                case enumOperation.Add:                    
                    if (CheckProcess("txtSTOREROOM_NM") == false) return false;
                    if (CheckNameAdd() == false) return false;
                    if (CheckProcess("txtSTOREROOM_PHONE") == false) return false;
                    if (CheckProcess("txtSTOREROOM_ADDRESS") == false) return false;
                    if (CheckProcess("cmbSTOREROOM_MANAGER") == false) return false;
                    if (CheckProcess("rdbSTOREROOM_STATE1") == false) return false;
                    if (CheckProcess("txtMEMO") == false) return false;
                    break; 
                case enumOperation.Update:
                    if (CheckProcess("txtSTOREROOM_ID") == false) return false;
                    if (CheckProcess("txtSTOREROOM_NM") == false) return false;
                    if (CheckNameUpdate() == false) return false;
                    if (CheckProcess("txtSTOREROOM_PHONE") == false) return false;
                    if (CheckProcess("txtSTOREROOM_ADDRESS") == false) return false;
                    if (CheckProcess("cmbSTOREROOM_MANAGER") == false) return false;
                    if (CheckProcess("rdbSTOREROOM_STATE1") == false) return false;
                    if (CheckProcess("txtMEMO") == false) return false;
                    break;
                case enumOperation.Delete:
                    if (CheckProcess("txtSTOREROOM_ID") == false) return false;
                    if (StorageRoomDeleteCheck(txtSTOREROOM_ID.Text.Trim()) == false)
                    {
                        return false;
                    }
                    break;
            }
            return true;
        }

        /// <summary>
        /// <p><b>方法名称</b>StorageRoomDeleteCheck</p>	
        /// <p><b>概要説明  :</b>检测仓库有没有被使用</p>
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/17</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" name="strStorageRoomID">仓库编码</param>
        /// <returns type = "bool"></returns>
        public bool StorageRoomDeleteCheck(string strStorageRoomID)
        {
            ReturnValue rvReturnValue =
                bll_M008_STOREROOM.StorageRoomDeleteCheck(strStorageRoomID);
            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return false;
            }
            if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count != 0)
            {
                Method.MessageShow("W11030", dataName);
                txtSTOREROOM_ID.Focus();
                txtSTOREROOM_ID.SelectAll();
                return false;
            }
            return true;
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckName</p>
        /// <p><b>概要説明  :</b>添加时仓库名称的Check</p>
        /// <p><b>作成者    :</b>万岗</p>
        /// <p><b>作成日    :</b>2008/09/17</p>
        /// <p><b>修改历史  :</b></p>
        /// </summary>
        /// <returns type = "bool">false:存在 true:不存在</returns>
        public bool CheckNameAdd()
        {
            ReturnValue rvReturnValue = bll_M008_STOREROOM.CheckName(txtSTOREROOM_NM.Text.Trim());
            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return false;
            }
            if (0 != rvReturnValue.ResultDataSet.Tables[0].Rows.Count)
            {
                Method.MessageShow("W11E01", dataName);//本[@]已经存在。
                txtSTOREROOM_NM.Focus();
                return false;
            }
            return true;
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckName</p>	
        /// <p><b>概要説明  :</b>更新时仓库名称的Check</p>
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/17</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <returns type = "bool">false:存在 true:不存在</returns>
        public bool CheckNameUpdate()
        {
            ReturnValue rvReturnValue = bll_M008_STOREROOM.CheckStoreRoomNameWithoutID(txtSTOREROOM_ID.Text.Trim(), txtSTOREROOM_NM.Text.Trim());
            if (!rvReturnValue.IsSuccess)
            {
                Method.MessageShow(rvReturnValue.ErrorID, dataName);
                return false;
            }
            else if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count > 0)
            {
                Method.MessageShow("W11E01", dataName);//本[@]已经存在。
                txtSTOREROOM_NM.Focus();
                return false;
            }
            return true;
        }

        #endregion        

        #region 事件
        
        private void M008_STOREROOM_Load(object sender, EventArgs e)
        {
            InitProcess();

            txtSTOREROOM_ID.Text = _storeRoomID;
            txtSTOREROOM_NM.Text = _storeRoomName;
            txtSTOREROOM_PHONE.Text  =_storeRoomPhone;
            txtSTOREROOM_ADDRESS.Text= _storeRoomAddress;
            txtMEMO.Text = _storeRoomMemo;

            if (!string.IsNullOrEmpty(_storeRoomState) && _storeRoomState == "0")
            {
                rdbSTOREROOM_STATE1.Checked = true;
                rdbSTOREROOM_STATE1.Checked = false;
            }
            else if (!string.IsNullOrEmpty(_storeRoomState) && _storeRoomState == "1")
            {
                rdbSTOREROOM_STATE1.Checked = false;
                rdbSTOREROOM_STATE1.Checked = true;
            }
            if (!string.IsNullOrEmpty(_storeRoomManager))
            {
                cmbSTOREROOM_MANAGER.SelectedValue = _storeRoomManager;
            }
            txtSTOREROOM_NM.Focus();
        }

        protected override void AllTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }

        private void AllTextBox_KeyPrecess(object sender, KeyPressEventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtSTOREROOM_PHONE":
                    FormBase.UKeyPress(e, InputType.Tel);
                    break;
            }
        }

        private void AllTextBox_GotFocus(object sender, EventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtSTOREROOM_ID":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    break;
                case "txtSTOREROOM_NM":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtMEMO":
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtSTOREROOM_ADDRESS":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtSTOREROOM_PHONE":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    break;              
            }
        }

        private void AllButton_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }

            string strButtonName = ((Button)sender).Name;

            switch (strButtonName)
            {
                #region btnAdd
                case "btnAdd":
                    {
                        if (txtSTOREROOM_ID.Text.Trim().Length < 1 && !this.CheckProcessLoop(enumOperation.Add))
                        {
                            return;
                        }
                        if (txtSTOREROOM_ID.Text.Trim().Length > 0 && !this.CheckProcessLoop(enumOperation.Update))
                        {
                            return;
                        }
                        Cursor.Current = Cursors.WaitCursor;
                        model.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID;
                        model.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD;
                        model.STOREROOM_ID = txtSTOREROOM_ID.Text;
                        model.STOREROOM_NM = txtSTOREROOM_NM.Text.Trim();
                        model.STOREROOM_PHONE = txtSTOREROOM_PHONE.Text.Trim();
                        model.MEMO = txtMEMO.Text.Trim();
                        model.STOREROOM_ADDRESS = txtSTOREROOM_ADDRESS.Text.Trim();
                        if (cmbSTOREROOM_MANAGER.Items.Count > 0 && cmbSTOREROOM_MANAGER.SelectedIndex >= 0)
                        {
                            model.STOREROOM_MANAGER = cmbSTOREROOM_MANAGER.SelectedValue.ToString();
                        }
                        else
                        {
                            model.STOREROOM_MANAGER = "";
                        }
                        if (rdbSTOREROOM_STATE1.Checked == true)
                        {
                            model.STOREROOM_STATE = "0";
                        }
                        if (rdbSTOREROOM_STATE2.Checked == true)
                        {
                            model.STOREROOM_STATE = "1";
                        }

                        ReturnValue result = bll_M008_STOREROOM.AddOrUpdateStoreRoom(model);
                        if (result.IsSuccess)
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                            Cursor.Current = Cursors.Default;
                            this.Close();
                        }
                        else
                        {
                            Method.MessageShow(result.ErrorID,dataName);
                            Cursor.Current = Cursors.Default;
                            txtSTOREROOM_NM.Focus();
                        }

                        break;
                    }
                #endregion

                #region btnExit
                case "btnExit":
                    this.Close();
                    break;
                #endregion

                #region btnClear
                case "btnClear":
                    {
                        txtSTOREROOM_NM.Text = "";
                        txtSTOREROOM_PHONE.Text = "";
                        txtSTOREROOM_ADDRESS.Text = "";
                        cmbSTOREROOM_MANAGER.SelectedIndex = 0;
                        rdbSTOREROOM_STATE1.Checked = true;
                        rdbSTOREROOM_STATE2.Checked = false;
                        txtMEMO.Text = "";
                        txtSTOREROOM_NM.Focus();
                    }
                    break;
                #endregion
            }

            Cursor.Current = Cursors.Default;
        }

        private void M008_STOREROOM_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    AllButton_Click(btnAdd, e);
                    break;
                case Keys.F12:
                    AllButton_Click(btnClear, e);
                    break;
                case Keys.Escape:
                    AllButton_Click(btnExit, e);
                    break;
            }          
        }        

        #endregion 
    }
}